package Formularios;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class frmLogin2 extends JDialog {
	private JTextField txtUsuario;
	private JPasswordField txtContraseña;
	
	private int iEmpleado;
	static Statement comando;
	static Connection con;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmLogin2 dialog = new frmLogin2();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public frmLogin2() {
		setTitle("Login");
		setBounds(100, 100, 279, 304);
		
		general gen = new general();
		try {
			comando = gen.abrirConexion(cNemonicos.IP_GLOBAL,1);
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		JLabel label = new JLabel("USUARIO:");
		
		txtUsuario = new JTextField();
		txtUsuario.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if ( arg0.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					txtContraseña.requestFocus();
				}
			}
		});
		txtUsuario.setColumns(10);
		
		JLabel label_1 = new JLabel("CONTRASEÑA :");
		
		txtContraseña = new JPasswordField();
		txtContraseña.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if ( arg0.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					if ( txtContraseña.getText().length() > 0)
					{
						String sUsuario = txtUsuario.getText();
						String sContraseña = txtContraseña.getText();
						int iNumEmpleado = general.validarUsuario(comando, sUsuario, sContraseña);
						System.out.println(iNumEmpleado);
						if ( iNumEmpleado != cNemonicos._ERROR_ )
						{
							iEmpleado = iNumEmpleado;
							dispose();
						}
						else
						{
							JOptionPane.showOptionDialog (null,"Usuario o Contraseña Invalidos","No Existe Usuario",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
						}
					}
				}
			}
		});
		txtContraseña.setColumns(10);
		
		JButton btnLogin = new JButton("Login");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(37)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtUsuario, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtContraseña, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(114)
							.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(85, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(62)
					.addComponent(label)
					.addGap(6)
					.addComponent(txtUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(label_1)
					.addGap(6)
					.addComponent(txtContraseña, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(29)
					.addComponent(btnLogin)
					.addContainerGap(117, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

	}
	
	public int getEmpleado()
	{
		int iEmpleado2 = iEmpleado;
		return iEmpleado2;
		
	}
}
